import api from '../api'

export const FETCH_PROFILE_PENDING = 'FETCH_PROFILE_PENDING';
export const FETCH_PROFILE_SUCCESS = 'FETCH_PROFILE_SUCCESS';

export const LOGIN_PENDING = 'LOGIN_PENDING';
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
export const LOGIN_ERROR = 'LOGIN_ERROR';

export const LOGOUT_SUCCESS = 'LOGOUT_SUCCESS';

// 取得登录文件
export function fetchProfile() {
  let uid = window.localStorage.getItem('uid');
  let p = window.localStorage.getItem('p');
  let ln = window.localStorage.getItem('ln');

  if (uid === undefined) {
      return {type: 'UID_NOT_FOUND'};
  }

  return {
      type: 'FETCH_PROFILE',
      payload: {
        promise: api.post('/login', {
          user: ln,
          password: p
        })
      }
  }
}

// 登录
export function login(user, password) {
  return {
      type: 'LOGIN',
      payload: {
        promise: api.post('/login', {
          user: user,
          password: password
        })
      }
  }
}

// 登出操作
export function logout() {

    return {
        type: 'LOGOUT',
        payload: {
          promise: api.get('/logout')
        }
    }
}
